<template>
  <el-dialog v-model="isShowDialog"
             :before-close="handleClose"
             :title="title"
             draggable
             width="700px"
             append-to-body
  >
    <el-form :model="saveParams" ref="formRef" label-width="100px" :rules="rules">
      <div style="display: flex;flex-wrap: wrap">
        <el-col :span="12">
          <el-form-item label="供应商编码" prop="vendorCode">
            <el-input v-model="saveParams.vendorCode" @input="saveParams.vendorCode=limitCharAndNumber(saveParams.vendorCode)" :disabled="isView" placeholder="供应商编码" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="供应商名称" prop="vendorName">
            <el-input v-model="saveParams.vendorName" :disabled="isView" placeholder="供应商名称" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属仓库" prop="warehouseId">
            <el-select
                :disabled="saveParams.vendorId"
                clearable
                v-model="saveParams.warehouseId"
            >
              <template v-for="(item,index) in warehouseList" :key="index">
                <el-option :label="item.warehouseName" :value="item.warehouseId"></el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系人">
            <el-input v-model="saveParams.linkman" :disabled="isView" placeholder="联系人" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="电话" >
            <el-input v-model="saveParams.phone" @input="saveParams.phone=limitIntNumber(saveParams.phone)" :maxlength="11" :disabled="isView" placeholder="电话" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="邮箱">
            <el-input v-model="saveParams.email" :disabled="isView" placeholder="邮箱" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="地址" >
            <el-input v-model="saveParams.address" :disabled="isView" placeholder="地址" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开户行" >
            <el-input v-model="saveParams.openBank" :disabled="isView" placeholder="开户行" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="账号" >
            <el-input v-model="saveParams.bankAccount" :disabled="isView" placeholder="账号" clearable/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="saveParams.remark" :disabled="isView" placeholder="备注" clearable/>
          </el-form-item>
        </el-col>
      </div>

    </el-form>

    <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="clickSave" v-if="!isView"> 保存 </el-button>
          <el-button @click="handleClose">取消</el-button>
        </span>
    </template>

  </el-dialog>
</template>

<script setup>
import {getCurrentInstance, onMounted} from "vue";
import {vendorSave} from "@/api/scApi";
import {limitCharAndNumber} from "@/utils/shuangcheng";
import {getWarehouseSelect} from "@/api/system/user";

const {proxy} = getCurrentInstance();
const props = defineProps({
  isShowDialog: {
    type: Boolean,
    default: false
  },
  title: {type: String},
  rowData:{type:Object},
  isView:{type:Boolean}
})
const emits = defineEmits(['cancel'])

function handleClose() {
  emits('cancel')
}

let saveParams = ref(JSON.parse(JSON.stringify(props.rowData)))
onMounted(()=>{
  if (!saveParams.value.status) {
    saveParams.value.status = 0
  }
  getWarehouse()
})
// 表单校验
const rules = ref({
  vendorCode: [{required: true, message: "供应商编码不能为空", trigger: "blur"}],
  vendorName: [{required: true, message: "供应商名称不能为空", trigger: "blur"}],
  warehouseId: [{required: true, message: "仓库不能为空", trigger: "change"}],
});

function clickSave() {

  proxy.$refs.formRef.validate((valid, fileds) => {
    if (valid) {
      vendorSave(saveParams.value).then(res => {
        if (res.code === 200) {
          proxy.$modal.msgSuccess('保存成功')
          emits('cancel', true)
        }
      })
    }
  })


}
const sysUserInfo=JSON.parse(localStorage.getItem("userInfo"))
//获取仓库下拉数据
let warehouseList = ref([])
function getWarehouse(){
  getWarehouseSelect({'companyId': sysUserInfo.companyId}).then(res => {
    warehouseList.value = res.data
  })
}
</script>

<style scoped lang="scss">

</style>